package com.cloud.controller;

import com.cloud.entity.GithubGetVo;
import com.cloud.entity.GithubItemVo;
import com.cloud.entity.RepositoryTopicVo;
import com.cloud.spider.user.service.TopicServiceImpl;
import com.cloud.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

@Controller
public class GitHubController {

    public static String[] langs = new String[]{
            "Scala", "Java", "Python", "Go", "JavaScript",
            "c", "r", "ruby", "rust", "php", "swift"
    };


    @Autowired
    TopicServiceImpl topicService;

    @RequestMapping("/api/qry")
    @ResponseBody
    public List<GithubItemVo> qryTopicWordCountByTopic(@RequestBody GithubGetVo githubGetVo) throws Exception {
        List<GithubItemVo> result = new ArrayList<>();
        String language = githubGetVo.getLanguage();
        Integer total = githubGetVo.getTotal();

        if (language == null || "".equals(language)) {
            StringBuilder sb = new StringBuilder();
            for (String lang : langs) {
                sb.append(lang + ",");
            }
            sb.delete(sb.length() - 1, sb.length());
            throw new Exception("language[" + sb.toString() + "]");
        }
        if (total < 0 || total > 100)
            throw new Exception("total[0,100]");

        for (String lang : langs) {
            if (lang.toLowerCase().equals(language.toLowerCase())) {
                String lan = language.toLowerCase();
                File wc = new File(FileUtil.getFilePath() + "wordcount/" + lan + ".txt");
                BufferedReader br = new BufferedReader(new FileReader(wc));
                for (int i = 0; i < total; i++) {
                    String line = br.readLine();
                    String[] split = line.split("\\s+");
                    String name = split[0];
                    int count = Integer.parseInt(split[1]);
                    result.add(new GithubItemVo(name, count));
                }
                return result;
            }
        }
        throw new Exception("language " + language + " don't support");
    }

    @RequestMapping("/api/qry/range")
    @ResponseBody
    public List<RepositoryTopicVo> qryRange(@RequestBody GithubGetVo githubGetVo) throws Exception {
        String language = githubGetVo.getLanguage();
        if (language == null || "".equals(language)) {
            StringBuilder sb = new StringBuilder();
            for (String lang : langs) {
                sb.append(lang + ",");
            }
            sb.delete(sb.length() - 1, sb.length());
            throw new Exception("language[" + sb.toString() + "]");
        }
        for (String lang : langs) {
            if (lang.toLowerCase().equals(language.toLowerCase())) {
                if (githubGetVo.getOnlyShowYear() == null) {
                    githubGetVo.setOnlyShowYear(false);
                }
                if (githubGetVo.getTotal() == null) {
                    return topicService.qryTopicsRange(language, 100, githubGetVo.getOnlyShowYear());
                }
                int total = githubGetVo.getTotal();
                if (total <= 0 || total > 100) {
                    total = 100;
                }
                return topicService.qryTopicsRange(language, total, githubGetVo.getOnlyShowYear());
            }
        }
        throw new Exception("language " + language + " don't support");
    }


}
